<!DOCTYPE html>
<html lang="zh">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>按大小分块显示</title>
    <style>
        body {
            font-family: 'Arial', sans-serif;
            background-color: #f4f7f6;
            color: #333;
            margin: 0;
            padding: 0;
            display: flex;
            justify-content: center;
            align-items: center;
            height: 100vh;
            text-align: center;
        }

        .container {
            background-color: white;
            padding: 20px;
            border-radius: 8px;
            box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
            width: 80%;
            max-width: 600px;
        }

        h1 {
            color: #4CAF50;
            margin-bottom: 20px;
        }

        .input-group {
            margin: 20px 0;
        }

        .input-group label {
            font-size: 18px;
            display: block;
            margin-bottom: 10px;
        }

        .input-group input {
            padding: 8px 25px;
            font-size: 16px;
            text-align: center;
            border: 1px solid #dedede;
            border-radius: 4px;
            outline: none;
        }

        .input-group input:focus {
            border-color: #4CAF50;
        }

        .btn {
            padding: 10px 20px;
            background-color: #4CAF50;
            color: white;
            border: none;
            border-radius: 4px;
            font-size: 16px;
            cursor: pointer;
            transition: background-color 0.3s;
        }

        .btn:hover {
            background-color: #45a049;
        }

        .result {
            margin-top: 30px;
            display: grid;
            grid-template-columns: repeat(3, 1fr);
            gap: 10px;
        }

        .chunk {
            background-color: #e8f5e9;
            padding: 15px;
            border-radius: 8px;
            box-shadow: 0 2px 5px rgba(0, 0, 0, 0.1);
        }

        .chunk span {
            display: block;
            padding: 5px;
            background-color: #81c784;
            color: white;
            border-radius: 4px;
            margin: 5px 0;
        }
    </style>
</head>

<body>
    <div class="container">
        <h1>数组分块示例</h1>
        <p>输入一个数组和分块大小，然后点击按钮查看结果。</p>

        <div class="input-group">
            <label for="arrayInput">数组 (用逗号分隔):</label>
            <input type="text" id="arrayInput" placeholder="1,2,3,4,5,6,7,8,9" />
        </div>

        <div class="input-group">
            <label for="sizeInput">分块大小:</label>
            <input type="number" id="sizeInput" value="3" min="1" />
        </div>

        <button class="btn" onclick="displayChunks()">分块显示</button>

        <div class="result" id="result"></div>
    </div>

    <script>
        const chunkify = function* (itr, size) {
            let chunk = [];
            for (const item of itr) {
                chunk.push(item);
                if (chunk.length === size) {
                    yield chunk;
                    chunk = [];
                }
            }
            if (chunk.length) {
                yield chunk;
            }
        };

        function displayChunks() {
            const arrayInput = document.getElementById("arrayInput").value;
            const sizeInput = parseInt(document.getElementById("sizeInput").value);

            // 将输入的数组转换为数字数组
            const array = arrayInput.split(',').map(item => item.trim()).filter(item => item !== '').map(Number);

            if (array.length === 0 || sizeInput <= 0) {
                alert("请输入有效的数组和分块大小！");
                return;
            }

            // 清空之前的结果
            const resultDiv = document.getElementById("result");
            resultDiv.innerHTML = '';

            // 使用 chunkify 函数进行分块
            const chunks = chunkify(array, sizeInput);
            for (const chunk of chunks) {
                const chunkDiv = document.createElement("div");
                chunkDiv.classList.add("chunk");

                chunk.forEach(item => {
                    const itemSpan = document.createElement("span");
                    itemSpan.textContent = item;
                    chunkDiv.appendChild(itemSpan);
                });

                resultDiv.appendChild(chunkDiv);
            }
        }
    </script>
</body>

</html>